package leetcode.medium.limit1900;

import leetcode.ListNode;

import java.util.HashMap;

public class No1836 {
    public static void main(String[] args) {

    }

    public ListNode deleteDuplicatesUnsorted(ListNode head) {
        HashMap<Integer, Integer> count = new HashMap<>();
        ListNode p = head;
        while (p != null) {
            count.put(p.val, count.getOrDefault(p.val, 0));
            p = p.next;
        }
        ListNode d = new ListNode(-1);
        d.next = head;
        p = d;
        while (p != null) {
            ListNode curr = p.next;
            while (curr != null && count.get(curr.val) > 1) {
                curr = curr.next;
            }
            p.next = curr;
            p = p.next;
        }
        return d.next;
    }
}
